﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Speech.Synthesis;
using System.Speech.AudioFormat;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Globalization;

namespace QingYun.TtsRecorder
{
    public class SystemSpeechTTS
    {
        // 播放TTS语音
        public static void PlayText(string text, string voiceName = "", int sampleRate = 16000, int bitsPerSample = 16,int speed=0, int channels = 1)
        {
            using (var synth = new SpeechSynthesizer())
            {
                var format = new SpeechAudioFormatInfo(
                    sampleRate,
                    (AudioBitsPerSample)bitsPerSample,
                    (AudioChannel)channels);

                if (!String.IsNullOrEmpty(voiceName))
                    synth.SelectVoice(voiceName);
                synth.Rate = speed;
                synth.SetOutputToDefaultAudioDevice(); // 输出到默认扬声器
                //synth.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Child); // 选择语音

                //多语音切换
                //var builder = new PromptBuilder();
                //builder.StartVoice(new CultureInfo("en-US"));
                //builder.AppendText("All we need to do is to keep talking.");
                //builder.EndVoice();
                //builder.StartVoice(new CultureInfo("ru-RU"));
                //builder.AppendText("Всё, что нам нужно сделать, это продолжать говорить");
                //builder.EndVoice();
                //synthesizer.Speak(builder);

                synth.Speak(text);
            }
        }

        // 输出TTS录音到WAV文件
        public static void SaveToWav(string text, string filePath, string voiceName = "", int sampleRate = 16000, int bitsPerSample = 16,int speed=0, int channels = 1)
        {
            using (var synth = new SpeechSynthesizer())
            {
                var format = new SpeechAudioFormatInfo(
                    sampleRate,
                    (AudioBitsPerSample)bitsPerSample,
                    (AudioChannel)channels);

                if (!String.IsNullOrEmpty(voiceName))
                    synth.SelectVoice(voiceName);
                synth.Rate = speed;
                synth.SetOutputToWaveFile(filePath, format);
                synth.Speak(text);
            }
        }

        public static List<string> ListInstalledVoices()
        {
            List<string> voiceNames = new List<string>();
            using (var synth = new SpeechSynthesizer())
            {
                Debug.WriteLine("已安装的语音列表:");
                foreach (var voice in synth.GetInstalledVoices())
                {
                    var info = voice.VoiceInfo;
                    Debug.WriteLine($"Id: {info.Id} | Name: {info.Name} | Age: {info.Age} | Gender: {info.Gender} | Culture: {info.Culture}");
                    voiceNames.Add(info.Name);
                }
                Debug.WriteLine("=================================================");

            }
            return voiceNames;
        }
        //        // 示例调用
        //        SystemSpeechTTS.PlayText("Hello, this is a test.", 22050, 16, 1); // 播放
        //        SystemSpeechTTS.SaveToWav("Hello, this is a test.", "output.wav", 44100, 16, 1); // 保存WAV
    }
}
